package cn.shyouchi.wuguanjia.android.ui.event.handover;

import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.Button;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.shyouchi.wuguanjia.android.R;
import cn.shyouchi.wuguanjia.android.base.BaseActivity;
import cn.shyouchi.wuguanjia.android.event.DataChangeEvent;
import cn.shyouchi.wuguanjia.android.utils.LogUtil;
import cn.shyouchi.wuguanjia.android.widget.TopBar;

public class HandoverManagerActivity extends BaseActivity implements HandoverContract.View {


    public final static int REQUEST_TYPE_ADD = 10001;
    public final static int REQUEST_TYPE_ADD_SUCCESS = 10001;

    @BindView(R.id.topBar)
    TopBar mTopBar;
    @BindView(R.id.rv_handover)
    RecyclerView mRvHandover;
    @BindView(R.id.btn_add_handover)
    Button mBtnAddHandover;

    List<HandoverEntity.ResultMessageBean> mHandoverEntities = new ArrayList<>();
    HandoverAdapter mAdapter;


    String projectId;

    HandoverPresenter mHandoverPresenter;

    boolean needRefresh = true;

    @Override
    public int initContentView() {
        return R.layout.activity_handover;
    }

    @Override
    public void initInjector() {
        mHandoverPresenter = new HandoverPresenter(mContext);
        mHandoverPresenter.attachView(this);
        projectId = getIntent().getStringExtra("projectId");
    }

    @Override
    public void initUiAndListener() {
        ButterKnife.bind(this);
        mTopBar.setLeftIcon(R.mipmap.ic_back);
        mTopBar.setTitle(getTitle().toString());
        setSupportActionBar(mTopBar.getToolbar());

        EventBus.getDefault().register(this);
        mRvHandover.setLayoutManager(new LinearLayoutManager(mContext));
        mAdapter = new HandoverAdapter(mHandoverEntities, this);
        mRvHandover.setAdapter(mAdapter);


    }

    @Override
    protected void onResume() {
        super.onResume();
        if (needRefresh) {
            mHandoverPresenter.getPrecaution(projectId);
            needRefresh = false;
        }
    }

    @Override
    protected boolean isApplyStatusBarTranslucency() {
        return false;
    }

    @Override
    protected boolean isApplyStatusBarColor() {
        return true;
    }


    @Override
    public void onDataRefresh(List<HandoverEntity.ResultMessageBean> resultMessages) {
        mHandoverEntities.clear();
        mHandoverEntities.addAll(resultMessages);
        mAdapter.notifyDataSetChanged();
    }

    @Override
    public void onItemClick(HandoverEntity.ResultMessageBean entity) {

        LogUtil.json(entity);

        Intent intent = new Intent(mContext, PreviewHandoverActivity.class);
        intent.putExtra("projectId", projectId);
        intent.putExtra("handover", entity);
        startActivity(intent);

    }

    @Override
    public void onDelClick(HandoverEntity.ResultMessageBean entity) {
        mHandoverPresenter.delPrecaution(projectId, String.valueOf(entity.getId()));
    }

    @OnClick(R.id.btn_add_handover)
    public void onAddHandoverClick() {
        Intent intent = new Intent(mContext, AddHandoverActivity.class);
        intent.putExtra("projectId", projectId);
        startActivityForResult(intent, REQUEST_TYPE_ADD);
    }

    @Override
    protected void onDestroy() {
        EventBus.getDefault().unregister(this);
        super.onDestroy();
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void DataChange(DataChangeEvent event) {
        needRefresh = true;
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == REQUEST_TYPE_ADD && resultCode == REQUEST_TYPE_ADD_SUCCESS) {
            mHandoverPresenter.getPrecaution(projectId);
        }
    }


}
